package com.itheima.core.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itheima.common.utils.Page;
import com.itheima.core.dao.CustomerDao;
import com.itheima.core.po.Customer;
import com.itheima.core.service.CustomerService;

/**
 *	@author zangye03
 *	date Jul 17, 20214:47:11 PM
 *	Description	 
 *	客户管理
 */

@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerDao customerDao;
	
	/**
	 * 客户列表
	 */
	@Override
	public Page<Customer> findCustomerList(Integer page, Integer rows, String custName, String custSource,
			String custIndustry, String custLevel) {
		// 创建客户对象
		Customer customer = new Customer();
		// 判断客户名称 
		// StringUtils.isNotBlank 	判断某字符串是否为空或长度为0或由空白符(whitespace)构成
		if (StringUtils.isNotBlank(custName)) {
			customer.setCustName(custName);
		}
		// 判断客户信息来源
		if (StringUtils.isNotBlank(custSource)) {
			customer.setCustSource(custSource);
		}
		// 判断客户所属行业
		if (StringUtils.isNoneBlank(custIndustry)) {
			customer.setCustIndustry(custIndustry);
		}
		// 判断客户级别
		if (StringUtils.isNotBlank(custLevel)) {
			customer.setCustLevel(custLevel);
		}
		// 当前页
		customer.setStart((page - 1) * rows);
		// 每页数
		customer.setRows(rows);
		// 查询客户列表
		List<Customer> customers = customerDao.selectCustomerList(customer);
			
		// 查询客户列表总记录数
		Integer count = customerDao.selectCustomerListCount(customer);
		// 创建Page返回对象
		Page<Customer> result = new Page<>();
		result.setPage(page);
		result.setRows(customers);
		result.setSize(rows);
		result.setTotal(count);
		return result;
	}

	/**
	 * 创建客户
	 */
	@Override
	public int createCustomer(Customer customer) {
		return customerDao.createCustomer(customer);
	}

	/**
	 * 通过id查询客户
	 */
	@Override
	public Customer getCustomerById(Integer id) {
		Customer customer = customerDao.getCustomerById(id);
		System.out.println(customer);
		return customer;
	}

	/**
	 * 更新客户
	 */
	@Override
	public int updateCustomer(Customer customer) {
		return customerDao.updateCustomer(customer);
	}

	/**
	 * 删除客户
	 */
	@Override
	public int deleteCustomer(Integer id) {
		return customerDao.deleteCustomer(id);
	}



}
